package com.aquarium.ui.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.aquarium.R;
import com.aquarium.config.ModuleData;
import com.aquarium.config.ProteinSkimmerData;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class ProteinSkimmerTimePicker extends ModuleTimePicker {

	private EditText mCleaningTime;
	private int mPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.protein_skimmer_time_picker);
		init();
	}

	@Override
	public void init() {
		super.init();
		if (mIntentAction.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
			mPosition = getIntent().getIntExtra(
					ControllerSetting.INTENT_EXTRA_LISTVIEW_CLICKED_POSITION, -1);
			if (mPosition != -1) {
				ProteinSkimmerData data = mModuleSettingDataBase
						.queryProteinSkimmerData(mModuleSettingDataBase
								.getCursor(
										ModuleSettingDataBase.PROTEIN_SKIMMER_TABLE,
										mPosition));
				setWidgetValue(data);
			}
		}
	}

	@Override
	public void setWidgetValue(ModuleData data) {
		super.setWidgetValue(data);
		mCleaningTime.setText(String
				.valueOf(((ProteinSkimmerData) data).duration));
	}

	@Override
	public void findViews() {
		super.findViews();
		mCleaningTime = (EditText) findViewById(R.id.duration_edtx);
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mOkButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ProteinSkimmerData data = new ProteinSkimmerData();
				setModuleData(data);
				data.duration = Integer.valueOf(mCleaningTime.getText()
						.toString());
				if (mIntentAction.equals(ControllerSetting.ACTION_ADD_SETTING)) {
					mModuleSettingDataBase.insertProteinSkimmerData(data);
				} else if (mIntentAction
						.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
					mModuleSettingDataBase.updateProteinSkimmerData(mPosition,
							data);
				}
				Intent intent = new Intent(
						ModuleControlService.ACTION_UPDATE_PROTEIN_SKIMMER_DATA);
				sendBroadcast(intent);
				finish();
			}
		});
		mCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}
